import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    // Toast标识
    let TOAST = "com.mazaiting/toast";
    
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
        ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
        // 获得跟窗口控制器
        let controller: FlutterViewController = window.rootViewController as! FlutterViewController
        // 获取方法通道
        let userDefault = FlutterMethodChannel(name: TOAST, binaryMessenger: controller)
        // 初始化方法
        self.initMethodChannel(userDefault: userDefault);
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    //
    // 初始化本地方法
    // @param userDefalut Flutter方法通道
    //
    func initMethodChannel(userDefault: FlutterMethodChannel) {
        userDefault.setMethodCallHandler {(call, result) in
//            NSLog("method: %s", call.method)
            switch call.method {
            case "showToast":
                // 参数获取
                var params: NSDictionary = call.arguments as! NSDictionary
                var msg: String = params.value(forKey: "msg") as! String
//                var args = call.arguments as! NSDictionary
//                var msg = args.value(forKey: "msg") as! String
                ToastView.instance.showToast(content: msg)
//                print(call.method)
                break
            default:
                result(FlutterMethodNotImplemented)
            }
            
            result("success")
        }
    }
    
    
}
